package com.tk.exam.web.admin;

import com.tk.exam.exception.ApiException;
import com.tk.exam.http.Response;
import com.tk.exam.service.TkExamManageService;
import com.tk.exam.utils.PageInfo;
import com.tk.exam.vo.exam.DelExamReq;
import com.tk.exam.vo.exam.SearchExamListRespVo;
import com.tk.exam.vo.exam.UpExamReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@RestController
@RequestMapping("tk/exam/admin/examManage")
@Slf4j
public class ExamManageController {
    @Autowired
    private TkExamManageService tkExamManageService;

    /**
     * 查询考试列表
     * @return
     */
    @RequestMapping(value = "/getExamList", method = RequestMethod.POST)
    public Response searchExamList() {
        List<SearchExamListRespVo> searchExamListRespVos = tkExamManageService.getExamList();
        return Response.succeed().put("examList",searchExamListRespVos);
    }

    /**
     * 新增考试
     * @param req
     * @return
     */
    @PostMapping(value = "addExam")
    public Response addExam(@RequestBody UpExamReq req){
        //非空校验
        addUpReqVali(req);
        tkExamManageService.addExam(req);
        return Response.succeed();
    }

    /**
     * 编辑考试信息
     * @param req
     * @return
     */
    @PostMapping(value = "/upExam")
    public Response upExam(@RequestBody UpExamReq req){
        //非空校验
        if(Objects.isNull(req.getId()))
            throw ApiException.of("id不能为空");
        addUpReqVali(req);
        tkExamManageService.upExam(req);
        return Response.succeed();
    }

    private void addUpReqVali(UpExamReq req){
        if(StringUtils.isBlank(req.getExamLocation()))
            throw ApiException.of("考试地点不能为空");
        if(StringUtils.isBlank(req.getExamName()))
            throw ApiException.of("考试名称不能为空");
        if(StringUtils.isBlank(req.getExamTime()))
            throw ApiException.of("考试时间不能为空");
        if(Objects.isNull(req.getExamTotalTime()))
            throw ApiException.of("考试总时间不能为空");
        if(Objects.isNull(req.getExamPaperId()))
            throw ApiException.of("选用试卷不能为空");
        if(Objects.isNull(req.getClassId()))
            throw ApiException.of("考试班级不能为空");
    }

    /**
     * 查询试卷列表
     * @return
     */
    @PostMapping(value = "getPaperList")
    public Response searchPaperListByPageWithPage(){
        //查询分页数据
        com.github.pagehelper.PageInfo<SearchExamListRespVo> pageInfoHelper = tkExamManageService.getListWithPage();
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }

    /**
     * 删除考试
     */
    @PostMapping(value = "delExam")
    public Response delExam(String[] examIds){
        if(examIds==null||examIds.length==0){
            return Response.fail("id不能为空");
        }
        List<String> idlist = Arrays.asList(examIds);
        if(CollectionUtils.isEmpty(idlist)){
            return Response.fail("id不能为空");
        }
        List<Integer> intIdlist = idlist.stream().map(id->Integer.valueOf(id)).collect(Collectors.toList());
        tkExamManageService.delExam(intIdlist);
        return Response.succeed();
    }


}
